package com.sensor.Fragment;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import com.landmesure.bluetoothchat.LandCommunicationService;
import com.landmesure.bluetoothchat.R;

import com.landmesure.bluetoothchat.databinding.FragmentInstallationBinding;
import com.landmesure.communication.CommunicationPlatform;
import com.landmesure.communication.ManagerFactory;
import com.sensor.Manager.WaterMonitorManager;


public class InstallationFragment extends BaseFragment {

    public static LandCommunicationService landCommunicationServiceInstance;    //设备管理服务实例

    private WaterMonitorManager waterMonitorManager;
    private Handler handler;

    private FragmentInstallationBinding fragmentInstallationBinding;
    private MyServiceConn mServiceConnection;
    private boolean mIsBind;
    private Context mContext;

    public InstallationFragment() {

    }

    class MyServiceConn implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder iBinder) {
            LandCommunicationService.MyBinder myBinder;
            myBinder = (LandCommunicationService.MyBinder) iBinder;
            mIsBind=true;
            landCommunicationServiceInstance = myBinder.getInstance();
            landCommunicationServiceInstance.setBaseManager(waterMonitorManager);

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //启动设备管理服务，并绑定它
        mContext = getActivity();
        Intent intent = new Intent(mContext, LandCommunicationService.class);
        mContext.startService(intent);
        mContext.bindService(intent, mServiceConnection=new MyServiceConn(), Context.BIND_AUTO_CREATE);
        handler = new MyHandler();
        CommunicationPlatform.handler = handler;
        waterMonitorManager = (WaterMonitorManager) ManagerFactory.getRealManager("水质监测", handler);
        fragmentInstallationBinding = FragmentInstallationBinding.inflate(getLayoutInflater());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = fragmentInstallationBinding.getRoot();
        init();
        return rootView;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        mContext.unbindService(mServiceConnection);
    }
    private class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
        }
    }

    protected void showTip(String messsage) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext).setTitle("提示")
                .setMessage(messsage);
        builder.create().show();


    }
    private void init() {
        fragmentInstallationBinding.yyxssSwitch.setOnCheckedChangeListener(new MyCheckedChangeListener());
        fragmentInstallationBinding.jxxjsSwitch.setOnCheckedChangeListener(new MyCheckedChangeListener());
        fragmentInstallationBinding.cgqqxSwitch.setOnCheckedChangeListener(new MyCheckedChangeListener());
        fragmentInstallationBinding.zdcjySwitch.setOnCheckedChangeListener(new MyCheckedChangeListener());
        fragmentInstallationBinding.zdcpkSwitch.setOnCheckedChangeListener(new MyCheckedChangeListener());
        fragmentInstallationBinding.zdcqxSwitch.setOnCheckedChangeListener(new MyCheckedChangeListener());
        fragmentInstallationBinding.cxcpkSwitch.setOnCheckedChangeListener(new MyCheckedChangeListener());
        fragmentInstallationBinding.cxcjySwitch.setOnCheckedChangeListener(new MyCheckedChangeListener());
        fragmentInstallationBinding.cxcqxSwitch.setOnCheckedChangeListener(new MyCheckedChangeListener());
        fragmentInstallationBinding.glpkSwitch.setOnCheckedChangeListener(new MyCheckedChangeListener());
        fragmentInstallationBinding.glqxSwitch.setOnCheckedChangeListener(new MyCheckedChangeListener());

    }

    private class MyCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            waterMonitorManager.sendCommand(getEcode(compoundButton.getId()), b ? "01" : "00");
        }
    }

    private Integer getEcode(int id) {
        switch (id) {
            case R.id.zdcjySwitch: {
                return waterMonitorManager.getEcodeByRole("浊度池进样数据接口");
            }
            case R.id.zdcpkSwitch: {
                return waterMonitorManager.getEcodeByRole("浊度池排空数据接口");
            }
            case R.id.zdcqxSwitch: {
                return waterMonitorManager.getEcodeByRole("浊度池清洗数据接口");
            }
            case R.id.cxcjySwitch: {
                return waterMonitorManager.getEcodeByRole("沉淀池进样数据接口");
            }
            case R.id.cxcpkSwitch: {
                return waterMonitorManager.getEcodeByRole("沉淀池排空数据接口");
            }
            case R.id.cxcqxSwitch: {
                return waterMonitorManager.getEcodeByRole("沉淀池清洗数据接口");
            }
            case R.id.glpkSwitch: {
                return waterMonitorManager.getEcodeByRole("管路排空数据接口");
            }
            case R.id.glqxSwitch: {
                return waterMonitorManager.getEcodeByRole("管路清洗数据接口");
            }
            case R.id.cgqqxSwitch: {
                return waterMonitorManager.getEcodeByRole("传感器清洗数据接口");
            }
            case R.id.jxxjsSwitch: {
                return waterMonitorManager.getEcodeByRole("纯水箱进水数据接口");
            }
            case R.id.yyxssSwitch: {
                return waterMonitorManager.getEcodeByRole("雨水箱上水数据接口");
            }

        }
        return null;
    }
}
